/*  
 * Copyright (c) 2008 Daniel Rinehart
 * http://danielr.neophi.com/
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.ninearrows.db
{
    import flash.data.SQLConnection;
    import flash.data.SQLMode;
    import flash.data.SQLResult;
    import flash.data.SQLStatement;
    import flash.errors.SQLError;
    import flash.events.SQLErrorEvent;
    import flash.events.SQLEvent;
    import flash.filesystem.File;
    import flash.net.SharedObject;
    
    import mx.controls.Alert;
    import mx.formatters.DateFormatter;
    import mx.logging.ILogger;
    import mx.logging.Log;

    /**
     * Class demonstrating the synchronous API for working with
     * SQLConnection and SQLStatement instances. The class
     * makes no attempt at error recovery and only logs
     * errors that occur.
     */
    public class Projects
    {
    	// DB接続フラグ
        [Bindable]
        public var connected:Boolean;
                
        private static var _myLogger:ILogger = Log.getLogger("com.database.Projects");
        
        private var _sqlConnection:SQLConnection;
        private var _sqlStatement:SQLStatement;

    	// ローカライズラベル
        private var _app_label_hash:Array;

	    private var _localSo:SharedObject = SharedObject.getLocal("9arrowsWBS");
	    private var _loginId:int = _localSo.data.id;

	    public var _currentProjectId:int;

        /**
         * ローカルデータベースファイル存在確認
         * @param databaseFile Database file location.
         * @return Boolean
         */
        public function checkDatabase(databaseFile:File, projectCount:int):Boolean
        {
            if ((databaseFile != null) && (databaseFile.exists))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /**
         * ネットワーク接続チェック
         * @return Boolean
         */
        public function checkNetwork():Boolean
        {
                return true;
        }

        /**
         * ローカルデータベース接続
         * @param databaseFile Database file location.
         */
        public function connect(databaseFile:File):void
        {
            try
            {
                _sqlConnection = new SQLConnection();
                _sqlConnection.open(databaseFile, SQLMode.UPDATE);
                // Unless an error was thrown we know we are connected
                // at this point.
                connected = true;
                _myLogger.debug("connected");
            }
            catch(sqlError:SQLError)
            {
                _myLogger.error(sqlError.toString());
            }
        }

        /**
         * ユーザ情報を取得
         */
        public function get_user(login_id:String):Object
        {
            try
            {
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.GET_USER_QUERY;
                _sqlStatement.parameters[":login_id"] = login_id;
                _sqlStatement.execute();
                
                var user:Object = _sqlStatement.getResult().data[0];
                
                return user;
            } catch(sqlError:SQLError) {
                _myLogger.error(sqlError.toString());
            }
            return new Object();
        }

        /**
         * プロジェクトデータ取得
         * @param projectID
         * @return Array
         */
        public function loadProject(projectID:int):Object
        {
            try
            {
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.PROJECT_QUERY;
                _sqlStatement.parameters[":id"] = projectID;
                _sqlStatement.execute();
                _myLogger.debug("project with id {0} loaded", projectID);
                
                var project:Object = _sqlStatement.getResult().data[0];
                
                // project users
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.PROJECTUSER_QUERY;
                _sqlStatement.parameters[":project_id"] = projectID;
                _sqlStatement.execute();
                var project_users:Array = _sqlStatement.getResult().data;
                var user_names:String   = '';
                for (var l:int=0; l<project_users.length; l++) {
                    var user:Object = project_users[l];
                    user_names += ((l==0) ? '' : ', ') + ((_loginId == user.user_id) ? '自分' : user.user_name);
                }
                
                project.users      = project_users;
                project.user_names = user_names;
                
                // A simplification is made to handle 0 products
                // loaded by returning an Array instead of testing
                // for length 0 and returning null or the Object loaded.
                return project;
            }
            catch(sqlError:SQLError)
            {
                _myLogger.error(sqlError.toString());
            }
            return new Object();
        }
        
        /**
         * プロジェクト一覧データ取得
         * @return Array
         */
        public function loadProjects():Array
        {
            try
            {
                _app_label_hash = new Array();
                var results:Array = new Array();
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.MESSAGE_LIST_QUERY;

                _sqlStatement.execute();
                results = _sqlStatement.getResult().data
                if (!results)
                {
                	return new Array();
                }
                for (var i:int = 0; i < results.length; i++)
                {
                	if (results[i]["msg_kbn"] == '1')
                	{
                		var key:String = "label_" + results[i]["msg_code"];
                		_app_label_hash[key] = results[i]["msg_jp"];
                	}
                }
            }
            catch(sqlError:SQLError)
            {
				Alert.show("SQLError:"+sqlError.toString(), "loadProjects", Alert.OK);
                _myLogger.error(sqlError.toString());
            }

            try
            {
                var results:Array = new Array();
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.PROJECT_LIST_QUERY;

                _sqlStatement.execute();
                var projects:Array = _sqlStatement.getResult().data;
                if (projects) {
                    for(var k:Number=0; k<projects.length; k++) {
                        var item:Object = projects[k];
                        // project users
                        _sqlStatement = new SQLStatement();
                        _sqlStatement.sqlConnection = _sqlConnection;
                        _sqlStatement.text = Statements.PROJECTUSER_QUERY;
                        _sqlStatement.parameters[":project_id"] = item.id;
                        _sqlStatement.execute();
                        var project_users:Array = _sqlStatement.getResult().data;
                        var user_names:String   = '';
                        var currentUser:Object = null;
                        for (var l:int=0; l<project_users.length; l++) {
                            var user:Object = project_users[l];
                            if (_loginId == user.user_id) {
                                currentUser = user;
                            }
                            user_names += ((l==0) ? '' : ', ') + ((_loginId == user.user_id) ? '自分' : user.user_name);
                        }
                        projects[k].users      = project_users;
                        projects[k].user_names = user_names;
                        projects[k].user       = currentUser;
                    }
                } else {
                    projects = new Array();
                }
                return projects;
            }
            catch(sqlError:SQLError)
            {
				Alert.show("SQLError:"+sqlError.toString(), "loadProjects", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
            return new Array();
        }
        
        
        /**
         * WBS一覧データ取得
         * @param projectId
         * @return Array
         */
        public function loadWbs(projectId:int):Array
        {
        	_currentProjectId = projectId;
//Alert.show("_currentProjectId:"+_currentProjectId, "loadWbs", Alert.OK);
            try
            {
                var results:Array = new Array();
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.WBS_LIST_QUERY;
                _sqlStatement.parameters[":project_id"] = projectId;

                _sqlStatement.execute();
                results = _sqlStatement.getResult().data;
                for (var i:int = 0; i < results.length; i++)
                {
	                for (var key:String in results[i])
	                {
	                	if (key == 'progress_kbn')
	                	{
	                		if (results[i][key] == '1')
	                		{
	                			var label_key:String = "label_progress_rate_1"; 
		                		results[i]["situation"] = _app_label_hash[label_key];
		                		results[i]["progress_sign"] = "";
	                		}
	                		else if (results[i][key] == '2')
	                		{
	                			var label_key:String = "label_progress_kbn_2"; 
		                		results[i]["situation"] = _app_label_hash[label_key];
		                		results[i]["progress_sign"] = "";
	                		}
	                		else if (results[i][key] == '3')
	                		{
	                			var label_key:String = "label_progress_kbn_3"; 
		                		results[i]["situation"] = _app_label_hash[label_key];
		                		results[i]["progress_sign"] = "レ";
	                		}
	                		else if (results[i][key] == '9')
	                		{
	                			var label_key:String = "label_progress_kbn_9"; 
		                		results[i]["situation"] = _app_label_hash[label_key];
		                		results[i]["progress_sign"] = "";
	                		}
	                		else
	                		{
		                		results[i]["situation"] = "";
		                		results[i]["progress_sign"] = "";
	                		}
	                	}
	                }
                }
                return results;
            }
            catch(sqlError:SQLError)
            {
				Alert.show("SQLError:"+sqlError.toString(), "loadWbs", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
            return new Array();
        }
        
        /**
         * WBS一覧データ取得
         * @param projectId
         * @return Array
         */
        public function loadMyTasks(projectId:int, userId:int):Array
        {
            _currentProjectId = projectId;
            try
            {
                var results:Array = new Array();
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.MYTASK_LIST_QUERY;
                _sqlStatement.parameters[":project_id"]   = projectId;
                _sqlStatement.parameters[":main_user_id"] = userId;

                _sqlStatement.execute();
                var r:SQLResult = _sqlStatement.getResult();
                results = r.data;
                if (!results) {
                    return results;
                }
                for (var i:int = 0; i < results.length; i++)
                {
                    for (var key:String in results[i])
                    {
                        if (key == 'progress_kbn')
                        {
                            if (results[i][key] == '1')
                            {
                                var label_key:String = "label_progress_rate_1"; 
                                results[i]["situation"] = _app_label_hash[label_key];
                                results[i]["progress_sign"] = "";
                            }
                            else if (results[i][key] == '2')
                            {
                                var label_key:String = "label_progress_kbn_2"; 
                                results[i]["situation"] = _app_label_hash[label_key];
                                results[i]["progress_sign"] = "";
                            }
                            else if (results[i][key] == '3')
                            {
                                var label_key:String = "label_progress_kbn_3"; 
                                results[i]["situation"] = _app_label_hash[label_key];
                                results[i]["progress_sign"] = "レ";
                            }
                            else if (results[i][key] == '9')
                            {
                                var label_key:String = "label_progress_kbn_9"; 
                                results[i]["situation"] = _app_label_hash[label_key];
                                results[i]["progress_sign"] = "";
                            }
                            else
                            {
                                results[i]["situation"] = "";
                                results[i]["progress_sign"] = "";
                            }
                        }
                    }
                }
                return results;
            }
            catch(sqlError:SQLError)
            {
                Alert.show("SQLError:"+sqlError.toString(), "loadWbs", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
            return new Array();
        }
        
        /**
         * プロジェクトユーザ取得
         * @param projectId
         * @return Array
         */
        public function loadProjectusers(projectId:int):Array
        {
//Alert.show("PROJECTUSER_QUERY:"+Statements.PROJECTUSER_QUERY, "loadProjectusers", Alert.OK);
            try
            {
                _app_label_hash = new Array();
                var results:Array = new Array();
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.PROJECTUSER_QUERY;
                _sqlStatement.parameters[":project_id"] = projectId;

                _sqlStatement.execute();
                var users:Array = _sqlStatement.getResult().data;
                for (var i:int = 0; i < users.length; i++) {
                    users[i].select_multi  = false;
                    users[i].select_single = false;
                }
                return users;
            }
            catch(sqlError:SQLError)
            {
				Alert.show("SQLError:"+sqlError.toString(), "loadProjectusers", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
            return new Array();
        }
        
        
        /**
         * タスクデータ取得
         * @param taskID
         * @return Array
         */
        public function loadTask(taskID:int):Array
        {
            //Alert.show("TASK_QUERY:"+Statements.TASK_QUERY, "loadTask", Alert.OK);
            try
            {
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.TASK_QUERY;
                _sqlStatement.parameters[":id"] = taskID;
                _sqlStatement.execute();
                _myLogger.debug("task with id {0} loaded", taskID);
    
                // A simplification is made to handle 0 products
                // loaded by returning an Array instead of testing
                // for length 0 and returning null or the Object loaded.
                //Alert.show("sqlStatement.getResult().data:"+sqlStatement.getResult().data, "loadTask", Alert.OK);
                var r:SQLResult = _sqlStatement.getResult();
                
                return r.data;
            }
            catch(sqlError:SQLError)
            {
				Alert.show("SQLError:"+sqlError.toString(), "loadTask", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
            return new Array();            
        }
                
        /**
         * タスクユーザ取得
         * @param taskId
         * @return Array
         */
        public function loadTaskusers(taskId:int):Array
        {
            //Alert.show("PROJECTUSER_QUERY:"+Statements.PROJECTUSER_QUERY, "loadProjectusers", Alert.OK);
            try
            {
                _app_label_hash = new Array();
                var results:Array = new Array();
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.TASKUSER_QUERY;
                _sqlStatement.parameters[":task_id"] = taskId;

                _sqlStatement.execute();
                var users:Array = _sqlStatement.getResult().data;
                if (users && users.length > 0) {
                    for (var i:int = 0; i < users.length; i++) {
                        users[i].select_multi  = false;
                        users[i].select_single = false;
                    }
                }
                return users;
            }
            catch(sqlError:SQLError)
            {
				Alert.show("SQLError:"+sqlError.toString(), "loadTaskusers", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
            return new Array();
        }

                
        /**
         * タスク削除
         * @param compId プロジェクト構成ID
         * @return Array of products loaded.
         */
        public function deleteTaskData(compId:int):Array
        {
            //Alert.show("PROJECTUSER_QUERY:"+Statements.PROJECTUSER_QUERY, "loadProjectusers", Alert.OK);
            try
            {
                _app_label_hash = new Array();
                var results:Array = new Array();
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.DELETE_TASK_DATA_QUERY;
                _sqlStatement.parameters[":id"] = compId;

                _sqlStatement.execute();
                return _sqlStatement.getResult().data;
            }
            catch(sqlError:SQLError)
            {
				Alert.show("SQLError:"+sqlError.toString(), "deleteTaskData", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
            return new Array();
        }


/**
 * タスクデータ取得
 * @param taskID
 * @return Array
 */
public function loadMilestone(milestone_id:int):Array
{
 //Alert.show("TASK_QUERY:"+Statements.TASK_QUERY, "loadTask", Alert.OK);
    try
    {
        _sqlStatement = new SQLStatement();
        _sqlStatement.sqlConnection     = _sqlConnection;
        _sqlStatement.text              = Statements.MILESTONE_LOAD_QUERY;
        _sqlStatement.parameters[":id"] = milestone_id;
        _sqlStatement.execute();
        _myLogger.debug("milestone with id {0} loaded", milestone_id);

        // A simplification is made to handle 0 products
        // loaded by returning an Array instead of testing
        // for length 0 and returning null or the Object loaded.
        //Alert.show("sqlStatement.getResult().data:"+sqlStatement.getResult().data, "loadTask", Alert.OK);
        return _sqlStatement.getResult().data;
    }
    catch(sqlError:SQLError)
    {
        Alert.show("SQLError:"+sqlError.toString(), "loadTask", Alert.OK);
        _myLogger.error(sqlError.toString());
    }
    return new Array();            
}

public function loadEvent(event_id:int):Array
{
 //Alert.show("TASK_QUERY:"+Statements.TASK_QUERY, "loadTask", Alert.OK);
    try
    {
        _sqlStatement = new SQLStatement();
        _sqlStatement.sqlConnection     = _sqlConnection;
        _sqlStatement.text              = Statements.EVENT_LOAD_QUERY;
        _sqlStatement.parameters[":id"] = event_id;
        _sqlStatement.execute();
        _myLogger.debug("milestone with id {0} loaded", event_id);

        // A simplification is made to handle 0 products
        // loaded by returning an Array instead of testing
        // for length 0 and returning null or the Object loaded.
        //Alert.show("sqlStatement.getResult().data:"+sqlStatement.getResult().data, "loadTask", Alert.OK);
        return _sqlStatement.getResult().data;
    }
    catch(sqlError:SQLError)
    {
        Alert.show("SQLError:"+sqlError.toString(), "loadTask", Alert.OK);
        _myLogger.error(sqlError.toString());
    }
    return new Array();            
}





 function resultHandler(event:SQLEvent):void
{
    var result:SQLResult = _sqlStatement.getResult();
    if (result != null)
    {
    }
}

function errorHandler(event:SQLErrorEvent):void
{
	Alert.show("event:"+event, "errorHandler", Alert.OK);
}               
        /**
         * タスク更新
         * @param 入力データオブジェクト
         * @return Boolean
         */
        public function updateTaskData(target:Object):Boolean
        {
//Alert.show("PROJECTUSER_QUERY:"+Statements.PROJECTUSER_QUERY, "updateTaskData", Alert.OK);
            try
            {
		        var d:Date = new Date();
		        var dateFormatter:DateFormatter = new DateFormatter();
		        //dateFormatter.formatString = 'YYYY/MM/DD HH:ii:ss';
	            var dateValue:String = d.getFullYear()+'/'+(d.getMonth()+1)+'/'+d.getDate()+' '+d.getHours()+':'+d.getMinutes();
                _app_label_hash = new Array();

                // 一旦削除
                var is_demo:Boolean = true;
                if (target.taskusers.length > 0) {
                    var _task_id:int        = target.project_tree_id;
                    var _deleted_tasks:Array = new Array();
                    if (typeof _deleted_tasks[_task_id] == 'undefined') {
	                    _sqlStatement = new SQLStatement();
                        _sqlStatement.sqlConnection = _sqlConnection;
                        _sqlStatement.text = Statements.DELETE_TASKUSERS_DATA_QUERY;
                        _sqlStatement.parameters[":task_id"] = target.id;
                        _sqlStatement.addEventListener(SQLEvent.RESULT, resultHandler);
                        _sqlStatement.addEventListener(SQLErrorEvent.ERROR, errorHandler);
                        _sqlStatement.execute();
                    }
                    var taskusers:Array = target.taskusers;
                    for (var i:int = 0; i < taskusers.length; i++) {
                        _sqlStatement = new SQLStatement();
                        _sqlStatement.sqlConnection = _sqlConnection;
                        _sqlStatement.text = Statements.INSERT_TASKUSERS_DATA_QUERY;
                        _sqlStatement.parameters[":task_id"] = taskusers[i].task_id;
                        _sqlStatement.parameters[":projectuser_id"] = taskusers[i].projectuser_id;
                        _sqlStatement.parameters[":create_user_id"] = _loginId;
                        _sqlStatement.parameters[":created_on"]     = dateValue;
                        _sqlStatement.execute();
                    }
                }

                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.UPDATE_PROJECTCOMP_DATA_QUERY;
                _sqlStatement.parameters[":id"] = target.project_tree_id;
                _sqlStatement.parameters[":item_name"] = target.item_name;
                _sqlStatement.parameters[":class_word1"] = target.class_word1;
                _sqlStatement.parameters[":class_word2"] = target.class_word2;
                _sqlStatement.parameters[":class_word3"] = target.class_word3;
		        _sqlStatement.parameters[":update_user_id"] = _loginId;
		        _sqlStatement.parameters[":updated_on"] = dateValue;
                _sqlStatement.execute();
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.UPDATE_TASK_DATA_QUERY;
                _sqlStatement.parameters[":id"] = target.id;
                _sqlStatement.parameters[":priority_kbn"] = target.priority_kbn;
                _sqlStatement.parameters[":plan_power"] = target.plan_power;
                _sqlStatement.parameters[":tani_kbn"] = target.tani_kbn;
                _sqlStatement.parameters[":complete_date"] = target.complete_date;
                _sqlStatement.parameters[":start_date"] = target.start_date;
                _sqlStatement.parameters[":end_date"] = target.end_date;
                _sqlStatement.parameters[":client_user_id"] = target.client_user_id;
                _sqlStatement.parameters[":main_user_id"]   = target.main_user_id;
                _sqlStatement.parameters[":result"] = target.result;
                _sqlStatement.parameters[":memo"] = target.memo;
		        _sqlStatement.parameters[":update_user_id"] = _loginId;
		        _sqlStatement.parameters[":updated_on"] = dateValue;
                _sqlStatement.parameters[":progress_kbn"] = target.progress_kbn;
                _sqlStatement.execute();

                return true;
            }
            catch(sqlError:SQLError)
            {
				Alert.show("SQLError:"+sqlError.toString(), "updateTaskData", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
            return true;
        }


        /**
         * タスク追加
         * @param 入力データオブジェクト
         * @return Boolean
         */
        public function insertTaskData(target:Object):Boolean
        {
//Alert.show("PROJECTUSER_QUERY:"+Statements.PROJECTUSER_QUERY, "loadProjectusers", Alert.OK);
            try
            {
		        var d:Date = new Date();
		        var dateFormatter:DateFormatter = new DateFormatter();
	            var dateValue:String = dateFormatter.format(d);
                _app_label_hash = new Array();
                var results:Array = new Array();

                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.MAX_LINE_NO_QUERY;
                _sqlStatement.parameters[":project_id"] = _currentProjectId;
                _sqlStatement.execute();
			    var hoge:SQLResult = _sqlStatement.getResult();
                var data:Array = hoge.data;
                var line_no:int = data[0].max_line_no + 1;
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.MAX_PROJECTCOMP_ID_QUERY;
                _sqlStatement.execute();
			    var result:SQLResult = _sqlStatement.getResult();
                var max_projectcomp_id:int = result.data[0].max_projectcomp_id + 1;
                

                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.INSERT_PROJECTCOMP_DATA_QUERY;
                _sqlStatement.parameters[":id"] = max_projectcomp_id;
                _sqlStatement.parameters[":project_id"] = _currentProjectId;
                _sqlStatement.parameters[":line_no"] = line_no;
                _sqlStatement.parameters[":task_kbn"] = 1;
                _sqlStatement.parameters[":item_name"] = target.item_name;
                _sqlStatement.parameters[":class_word1"] = target.class_word1;
                _sqlStatement.parameters[":class_word2"] = target.class_word2;
                _sqlStatement.parameters[":class_word3"] = target.class_word3;
                _sqlStatement.parameters[":create_user_id"] = _loginId;
                _sqlStatement.parameters[":created_on"] = dateValue;
                _sqlStatement.parameters[":update_user_id"] = _loginId;
                _sqlStatement.parameters[":updated_on"] = dateValue;
                _sqlStatement.parameters[":last_operation_kbn"] = 2;
                _sqlStatement.execute();
			    var result:SQLResult = _sqlStatement.getResult();
                var project_tree_id:int = result.lastInsertRowID;
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.INSERT_TASK_DATA_QUERY;
                _sqlStatement.parameters[":project_tree_id"] = max_projectcomp_id;
                _sqlStatement.parameters[":priority_kbn"] = target.priority_kbn;
                _sqlStatement.parameters[":plan_power"] = target.plan_power;
                _sqlStatement.parameters[":fcas_power"] = 0;
                _sqlStatement.parameters[":exp_power"] = 0;
                _sqlStatement.parameters[":tani_kbn"] = target.tani_kbn;
                _sqlStatement.parameters[":client_user_id"] = target.client_user_id;
                _sqlStatement.parameters[":main_user_id"] = target.main_user_id;
                _sqlStatement.parameters[":complete_date"] = target.complete_date;
                _sqlStatement.parameters[":progress_kbn"] = 0;
                _sqlStatement.parameters[":progress_rate"] = 0;
                _sqlStatement.parameters[":memo"] = target.memo;
                _sqlStatement.parameters[":task_cd"] = "";
                _sqlStatement.parameters[":start_date"] = target.start_date;
                _sqlStatement.parameters[":end_date"] = target.end_date;
                _sqlStatement.parameters[":result"] = target.result;
                _sqlStatement.parameters[":create_user_id"] = _loginId;
                _sqlStatement.parameters[":created_on"] = dateValue;
                _sqlStatement.parameters[":update_user_id"] = _loginId;
                _sqlStatement.parameters[":updated_on"] = dateValue;
                _sqlStatement.parameters[":last_operation_kbn"] = 2;
                _sqlStatement.execute();
            	
                if (target.taskusers != null && target.taskusers.length > 0)
                {
                	var taskusers:Array = target.taskusers;
					for (var i:int = 0; i < taskusers.length; i++)
					{
		                _sqlStatement = new SQLStatement();
		                _sqlStatement.sqlConnection = _sqlConnection;
		                _sqlStatement.text = Statements.INSERT_TASKUSERS_DATA_QUERY;
		                _sqlStatement.parameters[":task_id"] = taskusers[i].task_id;
		                _sqlStatement.parameters[":projectuser_id"] = taskusers[i].projectuser_id;
		                _sqlStatement.parameters[":create_user_id"] = _loginId;
		                _sqlStatement.parameters[":created_on"] = dateValue;
		                _sqlStatement.execute();
					}
                }

                return true;
            }
            catch(sqlError:SQLError)
            {
				Alert.show("SQLError:"+sqlError.toString(), "insertTaskData", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
            return true;
        }



        /**
         * Load a number of random products from the database. All loaded products
         * will be combined into a single Array and passed to the handler function. 
         * @param productCount Number of products to load.
         * @param maxProductID Maximum product ID in the database.
         * @return Array of products loaded.
         */
        public function insertUsers(users:Array):Array
        {
            try
            {
                var results:Array = new Array();
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.INSERT_USER_QUERY;
                for (var i:int = 0; i < users.length; i++)
                {
					for (var j:String in users[i])
					{
	                    _sqlStatement.parameters[":"+j] = users[i][j];
					}
                    _sqlStatement.execute();
                }
//	            sqlStatement.sqlConnection.commit();   
                return results;
            }
            catch(sqlError:SQLError)
            {
                _myLogger.error(sqlError.toString());
            }


            return new Array();
        }


        
        /**
         * Load a number of random products from the database. All loaded products
         * will be combined into a single Array and passed to the handler function. 
         * @param productCount Number of products to load.
         * @param maxProductID Maximum product ID in the database.
         * @return Array of products loaded.
         */
        public function insertProjects(projects:Array):Array
        {
            try
            {
                var results:Array = new Array();
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.INSERT_PROJECT_QUERY;
                
                // The same SQLStatement can be reused in the loop
                // since when the execute() call returns and the
                // result extracted it is no longer in use.
                for (var i:int = 0; i < projects.length; i++)
                {
					for (var j:String in projects[i])
					{
	                    _sqlStatement.parameters[":"+j] = projects[i][j];
					}
                    _sqlStatement.execute();
//		            sqlStatement.sqlConnection.commit();
                }
                
                _myLogger.debug("insertProjects", results.length);
	            //sqlStatement.sqlConnection.commit();   
                return results;
            }
            catch(sqlError:SQLError)
            {
				Alert.show("SQLError:"+sqlError.details, "insertProjects", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
            return new Array();
        }
        

        /**
         * Load a number of random products from the database. All loaded products
         * will be combined into a single Array and passed to the handler function. 
         * @param productCount Number of products to load.
         * @param maxProductID Maximum product ID in the database.
         * @return Array of products loaded.
         */
        public function insertProjectUsers(projectusers:Array):Array
        {
            try
            {
                var results:Array = new Array();
                var str:String = new String();
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.INSERT_PROJECT_USERS_QUERY;
                
                // The same SQLStatement can be reused in the loop
                // since when the execute() call returns and the
                // result extracted it is no longer in use.
                for (var i:int = 0; i < projectusers.length; i++)
                {
						str += "■";
					for (var j:String in projectusers[i])
					{
						str += "#";
						str += projectusers[i][j];
	                    _sqlStatement.parameters[":"+j] = projectusers[i][j];
					}
                    _sqlStatement.execute();
//		            sqlStatement.sqlConnection.commit();
                }
                
                _myLogger.debug("insertProjectUsers", results.length);
	            //sqlStatement.sqlConnection.commit();   
                return results;
            }
            catch(sqlError:SQLError)
            {
				Alert.show("SQLError:"+sqlError.details, "insertProjectUsers", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
            return new Array();
        }
        
        /**
         * Load a number of random products from the database. All loaded products
         * will be combined into a single Array and passed to the handler function. 
         * @param productCount Number of products to load.
         * @param maxProductID Maximum product ID in the database.
         * @return Array of products loaded.
         */
        public function insertProjectComps(projects:Array):Array
        {
            try
            {
                var results:Array = new Array();
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.INSERT_PROJECT_COMPS_QUERY;
                
                // The same SQLStatement can be reused in the loop
                // since when the execute() call returns and the
                // result extracted it is no longer in use.
                for (var i:int = 0; i < projects.length; i++)
                {
					for (var j:String in projects[i])
					{
	                    _sqlStatement.parameters[":"+j] = projects[i][j];
					}
                    _sqlStatement.execute();
//		            sqlStatement.sqlConnection.commit();
                }
                
                _myLogger.debug("insertProjectComps", results.length);
	            //sqlStatement.sqlConnection.commit();   
                return results;
            }
            catch(sqlError:SQLError)
            {
				Alert.show("SQLError:"+sqlError.details, "insertProjectComps", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
            return new Array();
        }
        
        
        /**
         * Load a number of random products from the database. All loaded products
         * will be combined into a single Array and passed to the handler function. 
         * @param productCount Number of products to load.
         * @param maxProductID Maximum product ID in the database.
         * @return Array of products loaded.
         */
        public function insertProjectLogs(projects:Array):Array
        {
            try
            {
                var results:Array = new Array();
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.INSERT_PROJECT_LOGS_QUERY;
                
                // The same SQLStatement can be reused in the loop
                // since when the execute() call returns and the
                // result extracted it is no longer in use.
                for (var i:int = 0; i < projects.length; i++)
                {
					for (var j:String in projects[i])
					{
	                    _sqlStatement.parameters[":"+j] = projects[i][j];
					}
                    _sqlStatement.execute();
//		            sqlStatement.sqlConnection.commit();
                }
                
                _myLogger.debug("insertProjectLogs", results.length);
	            //sqlStatement.sqlConnection.commit();   
                return results;
            }
            catch(sqlError:SQLError)
            {
				Alert.show("SQLError:"+sqlError.details, "insertProjectLogs", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
            return new Array();
        }


        /**
         * Load a number of random products from the database. All loaded products
         * will be combined into a single Array and passed to the handler function. 
         * @param productCount Number of products to load.
         * @param maxProductID Maximum product ID in the database.
         * @return Array of products loaded.
         */
        public function insertTasks(projects:Array):Array
        {
            try
            {
                var results:Array = new Array();
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.INSERT_TASKS_QUERY;
                
                // The same SQLStatement can be reused in the loop
                // since when the execute() call returns and the
                // result extracted it is no longer in use.
                for (var i:int = 0; i < projects.length; i++)
                {
					for (var j:String in projects[i])
					{
	                    _sqlStatement.parameters[":"+j] = projects[i][j];
					}
                    _sqlStatement.execute();
//		            sqlStatement.sqlConnection.commit();
                }
                
                _myLogger.debug("insertProjectLogs", results.length);
	            //sqlStatement.sqlConnection.commit();   
                return results;
            }
            catch(sqlError:SQLError)
            {
				Alert.show("SQLError:"+sqlError.details, "insertProjectLogs", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
            return new Array();
        }



        /**
         * Load a number of random products from the database. All loaded products
         * will be combined into a single Array and passed to the handler function. 
         * @param productCount Number of products to load.
         * @param maxProductID Maximum product ID in the database.
         * @return Array of products loaded.
         */
        public function insertTaskusers(projects:Array):Array
        {
            try
            {
                var results:Array = new Array();
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.INSERT_TASKUSERS_QUERY;
                
                // The same SQLStatement can be reused in the loop
                // since when the execute() call returns and the
                // result extracted it is no longer in use.
                for (var i:int = 0; i < projects.length; i++)
                {
                    for (var j:String in projects[i]) {
                        _sqlStatement.parameters[":"+j] = projects[i][j];
                    }
                    _sqlStatement.execute();
//		            sqlStatement.sqlConnection.commit();
                }
                
                _myLogger.debug("insertProjectLogs", results.length);
	            //sqlStatement.sqlConnection.commit();   
                return results;
            }
            catch(sqlError:SQLError)
            {
				Alert.show("SQLError:"+sqlError.details, "insertProjectLogs", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
            return new Array();
        }

        /**
         * Load a number of random products from the database. All loaded products
         * will be combined into a single Array and passed to the handler function. 
         * @param productCount Number of products to load.
         * @param maxProductID Maximum product ID in the database.
         * @return Array of products loaded.
         */
        public function insertMilestones(projects:Array):Array
        {
            try
            {
                var results:Array = new Array();
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.INSERT_MILESTONES_QUERY;
                
                // The same SQLStatement can be reused in the loop
                // since when the execute() call returns and the
                // result extracted it is no longer in use.
                for (var i:int = 0; i < projects.length; i++)
                {
					for (var j:String in projects[i])
					{
	                    _sqlStatement.parameters[":"+j] = projects[i][j];
					}
                    _sqlStatement.execute();
//		            sqlStatement.sqlConnection.commit();
                }
                
                _myLogger.debug("insertProjectLogs", results.length);
	            //sqlStatement.sqlConnection.commit();   
                return results;
            }
            catch(sqlError:SQLError)
            {
				Alert.show("SQLError:"+sqlError.details, "insertProjectLogs", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
            return new Array();
        }


        /**
         * Load a number of random products from the database. All loaded products
         * will be combined into a single Array and passed to the handler function. 
         * @param productCount Number of products to load.
         * @param maxProductID Maximum product ID in the database.
         * @return Array of products loaded.
         */
        public function insertEvents(projects:Array):Array
        {
            try
            {
                var results:Array = new Array();
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.INSERT_EVENTS_QUERY;
                
                // The same SQLStatement can be reused in the loop
                // since when the execute() call returns and the
                // result extracted it is no longer in use.
                for (var i:int = 0; i < projects.length; i++)
                {
					for (var j:String in projects[i])
					{
	                    _sqlStatement.parameters[":"+j] = projects[i][j];
					}
                    _sqlStatement.execute();
//		            sqlStatement.sqlConnection.commit();
                }
                
                _myLogger.debug("insertProjectLogs", results.length);
	            //sqlStatement.sqlConnection.commit();   
                return results;
            }
            catch(sqlError:SQLError)
            {
				Alert.show("SQLError:"+sqlError.details, "insertProjectLogs", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
            return new Array();
        }

        /**
         * Load a number of random products from the database. All loaded products
         * will be combined into a single Array and passed to the handler function. 
         * @param productCount Number of products to load.
         * @param maxProductID Maximum product ID in the database.
         * @return Array of products loaded.
         */
        public function insertEventusers(projects:Array):Array
        {
            try
            {
                var results:Array = new Array();
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.INSERT_EVENTUSERS_QUERY;
                
                // The same SQLStatement can be reused in the loop
                // since when the execute() call returns and the
                // result extracted it is no longer in use.
                for (var i:int = 0; i < projects.length; i++)
                {
					for (var j:String in projects[i])
					{
	                    _sqlStatement.parameters[":"+j] = projects[i][j];
					}
                    _sqlStatement.execute();
//		            sqlStatement.sqlConnection.commit();
                }
                
                _myLogger.debug("insertProjectLogs", results.length);
	            //sqlStatement.sqlConnection.commit();   
                return results;
            }
            catch(sqlError:SQLError)
            {
				Alert.show("SQLError:"+sqlError.details, "insertProjectLogs", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
            return new Array();
        }


        /**
         * Load a number of random products from the database. All loaded products
         * will be combined into a single Array and passed to the handler function. 
         * @param productCount Number of products to load.
         * @param maxProductID Maximum product ID in the database.
         * @return Array of products loaded.
         */
        public function insertMessages(projects:Array):Array
        {
            try
            {
                var results:Array = new Array();
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.INSERT_MESSAGES_QUERY;
                
                // The same SQLStatement can be reused in the loop
                // since when the execute() call returns and the
                // result extracted it is no longer in use.
                for (var i:int = 0; i < projects.length; i++)
                {
					for (var j:String in projects[i])
					{
	                    _sqlStatement.parameters[":"+j] = projects[i][j];
					}
                    _sqlStatement.execute();
//		            sqlStatement.sqlConnection.commit();
                }
                
                _myLogger.debug("insertMessages", results.length);
	            //sqlStatement.sqlConnection.commit();   
                return results;
            }
            catch(sqlError:SQLError)
            {
				Alert.show("SQLError:"+sqlError.details, "insertMessages", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
            return new Array();
        }




        /**
         * Load a number of random products from the database. All loaded products
         * will be combined into a single Array and passed to the handler function. 
         * @param productCount Number of products to load.
         * @param maxProductID Maximum product ID in the database.
         * @return Array of products loaded.
         */
        public function initUsers(users:Array):Array
        {
            try
            {
                var results:Array = new Array();
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.INIT_USER_QUERY;
                _sqlStatement.execute();
                _myLogger.debug("initUsers");
                
                results = insertUsers(users);
                return results;
            }
            catch(sqlError:SQLError)
            {
                _myLogger.error(sqlError.toString());
            }
            return new Array();
        }

        public function updateUserPassword(user_id:String, new_password:String):void
        {
            try {
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.UPDATE_PASSWORD_QUERY;
                _sqlStatement.parameters[":login_id"]     = user_id;
                _sqlStatement.parameters[":new_password"] = new_password;
                _sqlStatement.execute();
            } catch(sqlError:SQLError) {
                _myLogger.error(sqlError.toString());
            }
        }


        public function loadSyncTaskData(projectID:int):Object
        {
            var r:Object = new Object();
            r.project_id = projectID
            try {
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.SYNC_TASKS_DATA;
                _sqlStatement.parameters[":project_id"] = projectID;
                _sqlStatement.execute();
                var dat_tasks:Array = _sqlStatement.getResult().data;
                r.dat_tasks = dat_tasks;
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.SYNC_PROJECTCOMPS_DATA;
                _sqlStatement.parameters[":project_id"] = projectID;
                _sqlStatement.execute();
                var dat_projectcomps:Array = _sqlStatement.getResult().data;
                r.dat_projectcomps = dat_projectcomps;
                
                _sqlStatement = new SQLStatement();
                _sqlStatement.sqlConnection = _sqlConnection;
                _sqlStatement.text = Statements.SYNC_TASKUSERS_DATA;
                _sqlStatement.parameters[":project_id"] = projectID;
                _sqlStatement.execute();
                var dat_taskusers:Array = _sqlStatement.getResult().data;
                r.dat_taskusers = dat_taskusers;
            } catch (sqlError:SQLError) {
                _myLogger.error(sqlError.toString());
            }
            return r;
        }

        public function syncLocalProjectcompData(arr:Array):void
        {
            try {
                var len:int = arr.length;
                for (var i:int; i<len; i++) {
                    var o:Object = arr[i];
                    _sqlStatement = new SQLStatement();
                    _sqlStatement.sqlConnection = _sqlConnection;
                    _sqlStatement.text = Statements.EXISTS_PROJECTCOMP_QUERY;
                    _sqlStatement.parameters[":id"] = o.id;
                    _sqlStatement.execute();
                    
                    var r:Array  = _sqlStatement.getResult().data;
                    if (r && r.length > 0) {
                        var c:Object = r[0];
                        
                        _sqlStatement.text = Statements.UPDATE_SYNC_PROJECTCOMP_DATA_QUERY;
                        _sqlStatement.parameters[":id"] = o.id;
                        _sqlStatement.parameters[":item_name"] = o.item_name;
                        _sqlStatement.parameters[":class_word1"] = o.class_word1;
                        _sqlStatement.parameters[":class_word2"] = o.class_word2;
                        _sqlStatement.parameters[":class_word3"] = o.class_word3;
                        _sqlStatement.parameters[":update_user_id"] = o.update_user_id;
                        _sqlStatement.parameters[":updated_on"]     = o.updated_on;
                        _sqlStatement.execute();
                    } else {
                        _sqlStatement.text = Statements.INSERT_PROJECT_COMPS_QUERY;
                        for (var j:String in o) {
                            _sqlStatement.parameters[":"+j] = o[j];
                        }
                        _sqlStatement.execute();
                    }
                }
            } catch (sqlError:SQLError) {
                Alert.show("SQLError:"+sqlError.toString(), "syncLocalProjectcompData", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
        }

        public function syncLocalTaskData(arr:Array):void
        {
            try {
                var len:int = arr.length;
                for (var i:int; i<len; i++) {
                    var o:Object = arr[i];
                    _sqlStatement = new SQLStatement() ;
                    _sqlStatement.sqlConnection = _sqlConnection;
                    _sqlStatement.text = Statements.EXISTS_TASK_QUERY;
                    _sqlStatement.parameters[":id"] = o.id;
                    _sqlStatement.execute();
                    var res:SQLResult = _sqlStatement.getResult();
                    var r:Array  = res.data;
                    if (r && r.length > 0) {
                        var c:Object = r[0];
                        
                        _sqlStatement = new SQLStatement();
                        _sqlStatement.sqlConnection = _sqlConnection;
                        _sqlStatement.text = Statements.UPDATE_SYNC_TASK_DATA_QUERY;
                        _sqlStatement.parameters[":id"] = o.id;
                        _sqlStatement.parameters[":priority_kbn"] = o.priority_kbn;
                        _sqlStatement.parameters[":plan_power"] = o.plan_power;
                        _sqlStatement.parameters[":tani_kbn"] = o.tani_kbn;
                        _sqlStatement.parameters[":complete_date"] = o.complete_date;
                        _sqlStatement.parameters[":start_date"] = o.start_date;
                        _sqlStatement.parameters[":end_date"]   = o.end_date;
                        _sqlStatement.parameters[":main_user_id"] = o.main_user_id;
                        _sqlStatement.parameters[":client_user_id"] = o.client_user_id;
                        _sqlStatement.parameters[":result"] = o.result;
                        _sqlStatement.parameters[":memo"]   = o.memo;
                        _sqlStatement.parameters[":update_user_id"] = o.update_user_id;
                        _sqlStatement.parameters[":updated_on"]    = o.updated_on;
                        _sqlStatement.parameters[":last_operation_kbn"]    = o.last_operation_kbn;
                        _sqlStatement.parameters[":progress_rate"]    = o.progress_rate;
                        _sqlStatement.parameters[":progress_kbn"]    = o.progress_kbn;
                        _sqlStatement.execute();
                    } else {
                        _sqlStatement = new SQLStatement();
                        _sqlStatement.sqlConnection = _sqlConnection;
                        _sqlStatement.text = Statements.INSERT_SYNC_TASK_DATA_QUERY;
                        var params:Array = ['id', 'project_tree_id', 'priority_kbn', 'plan_power', 'fcas_power', 'exp_power', 'tani_kbn',
                            'client_user_id', 'main_user_id', 'complete_date', 'progress_kbn', 'progress_rate', 'memo', 'task_cd',
                            'start_date', 'end_date', 'result', 'create_user_id', 'created_on', 'update_user_id', 'updated_on', 'last_operation_kbn'];
                        for (var j:String in params) {
                            _sqlStatement.parameters[":"+params[j]] = o[params[j]];
                        }
                        //_sqlStatement.parameters[":local_id"] = line_no;
                        _sqlStatement.execute();
                    }
                }
            } catch (sqlError:SQLError) {
                Alert.show("SQLError:"+sqlError.toString(), "syncLocalTaskData", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
        }

        public function syncLocalTaskUsersData(arr:Array):void
        {
            try {
                var deleted_tasks:Array = new Array();
                var len:int = arr.length;
                for (var i:int; i<len; i++) {
                    var o:Object = arr[i];
                    if (typeof deleted_tasks[o.task_id] == 'undefined') {
                        // 一旦削除
                        _sqlStatement = new SQLStatement();
                        _sqlStatement.sqlConnection = _sqlConnection;
                        _sqlStatement.text = Statements.DELETE_TASKUSERS_DATA_QUERY;
                        _sqlStatement.parameters[":task_id"] = o.task_id;
                        _sqlStatement.execute();
                        deleted_tasks[o.task_id] = true;
                    }
                    _sqlStatement = new SQLStatement();
                    _sqlStatement.sqlConnection = _sqlConnection;
                    _sqlStatement.text = Statements.INSERT_TASKUSERS_QUERY;
                    for (var j:String in o) {
                        _sqlStatement.parameters[":"+j] = o[j];
                    }
                    _sqlStatement.execute();
                }
            } catch (sqlError:SQLError) {
                Alert.show("SQLError:"+sqlError.toString(), "syncLocalTaskUsersData", Alert.OK);
                _myLogger.error(sqlError.toString());
            }
        }


        /**
         * Disconnect from the database. 
         */
        public function disconnect():void
        {
            try
            {
                if (_sqlConnection) {
                    _sqlConnection.close();
                }
                _sqlConnection = null;
                connected = false;
                _myLogger.debug("disconnected");
            }
            catch(sqlError:SQLError)
            {
                _myLogger.error(sqlError.toString());
            }
        }
    }
}